<?php

namespace App\Services;

use Dcat\Admin\Admin;
use Exception;
use App\Models\Config;
use Illuminate\Support\Facades\Mail;

class AiZhanService
{

    private $config = [];
    private $batch =[
        'baidu'=>"https://apistore.aizhan.com/baidurank/siteinfos/",
        '360'=>"https://apistore.aizhan.com/sorank/siteinfos/",
        'shenma'=>"https://apistore.aizhan.com/smrank/siteinfos/",
        'sougou'=>"https://apistore.aizhan.com/sogourank/siteinfos/",
    ];
    private $icpurl=[
        'icp'=>'https://apistore.aizhan.com/site/icpinfos/',
        'app'=>'https://apistore.aizhan.com/icp/app/',
    ];
    private $intercepturl='https://apistore.aizhan.com/site/intercept/';//域名拦截检测
    private $key='7556b24c76905b81b365ab2a7bc559d3';//百度网站权重

    /**
     * @throws Exception
     */
    public function __construct()
    {

    }



    public function deviceQuit(string $uid, int $device_flag = 0)
    {

        $data = [
            'uid' => $uid,
            'device_flag' => $device_flag,// 需要踢出的设备标记 -1: 当前用户下所有设备 0： 当前用户下的app 1： 当前用户下的web 2： 当前用户下的pc
        ];
        try {
            $response = \Illuminate\Support\Facades\Http::post($url, $data);
            $data = $response->json();
            if ($response->status() == 200) {
                return $data;
            } else {
                throw new Exception($data['message']);
            }
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    /***
     * 获取权重
     * @param string $tourl 多个链接地址用|分割
     * @return mixed
     * @throws Exception
     */
    public function quanz($type = 'baidu', $tourl)
    {
        $url=$this->quanztype($type);
        $url=$url.$this->key;
        try {
            $response = \Illuminate\Support\Facades\Http::get($url, ['domains' => $tourl]);
            $data = $response->json();
            if ($response->status() == 200) {
                if ($data['code'] == 200000) {
                    return $data['data'];
                } else {
                    throw newException($data['msg']);
                }
            }
        }catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function icpbeian($tourl)
    {
        $url=$this->beiantype('icp');
        $url=$url.$this->key;
        try {
            $response = \Illuminate\Support\Facades\Http::get($url, ['domains' => $tourl]);
            $data = $response->json();
            if ($response->status() == 200) {
                if ($data['code'] == 200000) {
                    return $data['data'];
                } else {
                    throw newException($data['msg']);
                }
            }
        }catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function quanztype($type)
    {
        $keys=array_keys($this->batch);
        if(!in_array($type,$keys)){
            throw new Exception('类型错误');
        }
        return $this->batch[$type];
    }
    public function beiantype($type)
    {
        $keys=array_keys($this->icpurl);
        if(!in_array($type,$keys)){
            throw new Exception('类型错误');
        }
        return $this->icpurl[$type];
    }

    public function intercept($tourl)
    {
        $url=$this->intercepturl.$this->key;
        try {
            $response = \Illuminate\Support\Facades\Http::get($url, ['domains' => $tourl]);
            $data = $response->json();
            if ($response->status() == 200) {
                if ($data['code'] == 200000) {
                    return $data['data'];
                } else {
                    throw newException($data['msg']);
                }
            }
        }catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

    }
}
